package com.happyfamily.springboot.crowdsourcingplatform.controller.pan;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import java.awt.image.BufferedImage;
import java.io.IOException;


/**
 *
 */
@Controller
@Slf4j
public class SecurityController
{

	//    private Logger logger=Logger.getLogger(TestController.class.getName());

	private DefaultKaptcha captchaProducer;

	public SecurityController(DefaultKaptcha kaptcha) {
		this.captchaProducer=kaptcha;
	}


	@GetMapping(value = "/back-login")
	public String loginBack() {
		return "backPage/back-login";
	}

	@GetMapping(value = "/404.html")
	public String noPermission() {
		return "404";
	}


	@GetMapping(value = "/user-login")
	public String loginFront() {
		return "frontPage/user-login";
	}


	@GetMapping(value = "/session/invalid")
	@ResponseStatus(HttpStatus.UNAUTHORIZED)
	@ResponseBody
	public String sessionInvalid(){

		return "session已失效，请重新认证";
	}


	/**
	 * 后端退出
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		//注销当前的session
		session.invalidate();

		return "/backPage/back-login";
	}


	/**
	 * 前端退出
	 * @return
	 */
	@RequestMapping("/frontLogout")
	public String logoutFront(HttpSession session) {
		//注销当前的session
		session.invalidate();

		return "/frontPage/user-login";
	}


	/**
	 * 生成验证码
	 */
	@RequestMapping(value = "/randCode")
	public void getRandCode(HttpServletRequest request, HttpServletResponse response){

		HttpSession session = request.getSession();


		response.setDateHeader("Expires", 0);

		// Set standard HTTP/1.1 no-cache headers.
		response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");

		// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
		response.addHeader("Cache-Control", "post-check=0, pre-check=0");

		// Set standard HTTP/1.0 no-cache header.
		response.setHeader("Pragma", "no-cache");

		// 设置返回文件类型
		response.setContentType("image/jpeg");

		// 获取验证码上的文字
		String capText = captchaProducer.createText();

		// 将验证码上的文字保存在session中
		session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);

		String code = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);

		log.info("验证码为:"+code);

		//  将文件渲染到图片上
		BufferedImage bi = captchaProducer.createImage(capText);
		ServletOutputStream out = null;
		try {
			out = response.getOutputStream();
			ImageIO.write(bi, "jpeg", out);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}


	}


}
